package com.mac.hosp_pre.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mac.hosp_pre.admin.constant.HospConstant;
import com.mac.hosp_pre.admin.entity.HospitalConfig;
import com.mac.hosp_pre.admin.enums.HospResultCodeEnum;
import com.mac.hosp_pre.admin.mapper.HospitalConfigMapper;
import com.mac.hosp_pre.admin.model.req.HospitalConfigReqDto;
import com.mac.hosp_pre.admin.model.req.HospitalQueryReqDto;
import com.mac.hosp_pre.admin.model.resp.HospitalConfigRespDto;
import com.mac.hosp_pre.admin.model.resp.SignInfoRespDto;
import com.mac.hosp_pre.admin.service.HospitalConfigService;
import com.mac.hosp_pre.except.ResultCodeEnum;
import com.mac.hosp_pre.except.ServiceException;
import com.mac.hosp_pre.util.IdGenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service
public class HospitalConfigServiceImpl extends ServiceImpl<HospitalConfigMapper, HospitalConfig> implements HospitalConfigService {

    @Override
    public String getSignKey(String hospCode) {
        LambdaQueryWrapper<HospitalConfig> wrapper = new LambdaQueryWrapper<HospitalConfig>().eq(HospitalConfig::getHospCode,hospCode);
        HospitalConfig HospitalConfig = baseMapper.selectOne(wrapper);
        return HospitalConfig.getSignKey();
    }

    @Override
    public SignInfoRespDto getSignInfo(String hospCode) {
        HospitalConfig config = baseMapper.selectOne(new LambdaQueryWrapper<HospitalConfig>().eq(HospitalConfig::getHospCode,hospCode));
        if(null == config) {
            throw new ServiceException(HospResultCodeEnum.HOSPITAL_OPEN.getCode(), HospResultCodeEnum.HOSPITAL_OPEN.getMsg());
        }
        SignInfoRespDto respDto = new SignInfoRespDto();
        respDto.setApiUrl(config.getApiUrl());
        respDto.setSignKey(config.getSignKey());
        return respDto;
    }

    @Override
    public IPage<HospitalConfigRespDto> findPageHospConfig(HospitalQueryReqDto hospitalSetQuery) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalConfig> page = new Page<>(hospitalSetQuery.getPageNo(),hospitalSetQuery.getPageSize());
        //构建条件
        LambdaQueryWrapper<HospitalConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(hospitalSetQuery.getHospName()), HospitalConfig::getHospName,hospitalSetQuery.getHospName());
        wrapper.eq(StringUtils.hasText(hospitalSetQuery.getHospCode()), HospitalConfig::getHospCode,hospitalSetQuery.getHospCode());
        wrapper.orderByDesc(HospitalConfig::getId);
        //调用方法实现分页查询
        IPage<HospitalConfig> pageHospitalSet = this.page(page, wrapper);
        if (CollectionUtil.isNotEmpty(pageHospitalSet.getRecords())){
            return pageHospitalSet.convert(x -> {
                HospitalConfigRespDto hospitalConfigRespDto = new HospitalConfigRespDto();
                BeanUtils.copyProperties(x, hospitalConfigRespDto);
                return hospitalConfigRespDto;
            });
        }
        return Page.of(hospitalSetQuery.getPageNo(),hospitalSetQuery.getPageSize(), 0);
    }

    @Override
    public void add(HospitalConfigReqDto hospitalConfigReq) {
        HospitalConfig hospitalSet = new HospitalConfig();
        BeanUtils.copyProperties(hospitalConfigReq, hospitalSet);
        //设置状态 1 使用 0 不能使用
        hospitalSet.setState(1);
        //签名秘钥
        hospitalSet.setSignKey(IdGenUtils.snowflakeStrId(HospConstant.HOSPCONFIG_SIGN_PREFIX));
        this.save(hospitalSet);
    }

    @Override
    public HospitalConfigRespDto getHospConfig(Long id) {
        HospitalConfig hospitalSet = this.getById(id);
        if (null != hospitalSet) {
            HospitalConfigRespDto hospitalConfigRespDto = new HospitalConfigRespDto();
            BeanUtils.copyProperties(hospitalSet, hospitalConfigRespDto);
            return hospitalConfigRespDto;
        }
        return null;
    }

    @Override
    public void updateHospitalConfig(HospitalConfigReqDto hospitalSet) {
        if (hospitalSet.getId() == null){
            throw new ServiceException(ResultCodeEnum.ID_EMPTY_ERROR);
        }
        HospitalConfig hospital = this.getById(hospitalSet.getId());
        if (hospital == null){
            throw new ServiceException(ResultCodeEnum.NOT_EXISTS_ERROR);
        }
        this.updateById(hospital);
    }

    @Override
    public void lockHospitalConfig(Long id, Integer status) {
        //根据id查询医院配置信息
        HospitalConfig hospitalSet = this.getById(id);
        if (hospitalSet == null){
            throw new ServiceException(ResultCodeEnum.NOT_EXISTS_ERROR);
        }
        //设置状态
        hospitalSet.setState(status);
        //调用方法
        this.updateById(hospitalSet);
    }

    @Override
    public void sendKey(Long id) {
        HospitalConfig hospitalSet = this.getById(id);
        if (hospitalSet == null){
            throw new ServiceException(ResultCodeEnum.NOT_EXISTS_ERROR);
        }
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHospCode();
        //TODO 发送短信
    }
}
